//
//  RectView.m
//  CIRectangleDetectDemo
//
//  Created by fujikoli(李鑫) on 2017/8/29.
//  Copyright © 2017年 fujikoli(李鑫). All rights reserved.
//

#import "RectView.h"

#define RectView_PanViewSize 20.f
@interface RectView ()
///推拽触摸锚点
@property(nonatomic,strong)UIView*buttonTL;
@property(nonatomic,strong)UIView*buttonTR;
@property(nonatomic,strong)UIView*buttonBL;
@property(nonatomic,strong)UIView*buttonBR;
@end

@implementation RectView

- (instancetype)init {
    if(self = [super init]) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
////////////////描绘矩形区域
- (void)drawRect:(CGRect)rect;
{
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (context)
    {
        CGContextMoveToPoint(context, _point1.x, _point1.y);
        CGContextAddLineToPoint(context, _point2.x, _point2.y);
        CGContextAddLineToPoint(context, _point3.x, _point3.y);
        CGContextAddLineToPoint(context, _point4.x, _point4.y);
        CGContextAddLineToPoint(context, _point1.x, _point1.y);
        CGContextSetRGBStrokeColor(context, 83 /255.0, 239/255.0, 111/255.0, 1);//green
        CGContextSetLineJoin(context, kCGLineJoinRound);
        CGContextSetLineWidth(context, 2.0f);
        CGContextStrokePath(context);
    }
}

- (void)drawWithPointsfirst:(CGPoint)point1 second:(CGPoint)point2 thrid:(CGPoint)point3 forth:(CGPoint)point4 {
    _point1 = point1;
    _point2 = point2;
    _point3 = point3;
    _point4 = point4;
}
/////////////托动顶点自定义矩形框
-(void)customPan{
    self.buttonTL.center=self.point1;
    self.buttonTR.center=self.point2;
    self.buttonBR.center=self.point3;
    self.buttonBL.center=self.point4;
}
-(UIView *)buttonTL{
    if(!_buttonTL){
        _buttonTL=[[UIView alloc]init];
        _buttonTL.bounds = CGRectMake(0, 0, RectView_PanViewSize, RectView_PanViewSize);
        _buttonTL.layer.cornerRadius = RectView_PanViewSize/2.0;
        _buttonTL.layer.borderColor = [UIColor redColor].CGColor;
        _buttonTL.layer.borderWidth = 1.f;
        _buttonTL.tag = 1;
        UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(movingBtn:)];
        [_buttonTL addGestureRecognizer:pan];
        [self addSubview:_buttonTL];
    }
    return _buttonTL;
}
-(UIView *)buttonTR{
    if(!_buttonTR){
        _buttonTR=[[UIView alloc]init];
        _buttonTR.bounds = CGRectMake(0, 0, RectView_PanViewSize, RectView_PanViewSize);
        _buttonTR.layer.cornerRadius = RectView_PanViewSize/2.0;
        _buttonTR.layer.borderColor = [UIColor yellowColor].CGColor;
        _buttonTR.layer.borderWidth = 1.f;
        _buttonTR.tag = 2;
        UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(movingBtn:)];
        [_buttonTR addGestureRecognizer:pan];
        [self addSubview:_buttonTR];
    }
    return _buttonTR;
}
-(UIView *)buttonBR{
    if(!_buttonBR){
        _buttonBR=[[UIView alloc]init];
        _buttonBR.bounds = CGRectMake(0, 0, RectView_PanViewSize, RectView_PanViewSize);
        _buttonBR.layer.cornerRadius = RectView_PanViewSize/2.0;
        _buttonBR.layer.borderColor = [UIColor blueColor].CGColor;
        _buttonBR.layer.borderWidth = 1.f;
        _buttonBR.tag = 3;
        UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(movingBtn:)];
        [_buttonBR addGestureRecognizer:pan];
        [self addSubview:_buttonBR];
    }
    return _buttonBR;
}
-(UIView *)buttonBL{
    if(!_buttonBL){
        _buttonBL=[[UIView alloc]init];
        _buttonBL.bounds = CGRectMake(0, 0, RectView_PanViewSize, RectView_PanViewSize);
        _buttonBL.layer.cornerRadius = RectView_PanViewSize/2.0;
        _buttonBL.layer.borderColor = [UIColor blackColor].CGColor;
        _buttonBL.layer.borderWidth = 1.f;
        _buttonBL.tag = 4;
        UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(movingBtn:)];
        [_buttonBL addGestureRecognizer:pan];
        [self addSubview:_buttonBL];
    }
    return _buttonBL;
}
-(void)movingBtn:(UIPanGestureRecognizer *)recognizer{
    UIView *button = recognizer.view;
    //得到拖拽的偏移量
    CGPoint translation = [recognizer translationInView:button];
    //对拖拽事件的状态进行判断 也就是选择自定义手势的原因
    if(recognizer.state == UIGestureRecognizerStateBegan){
         
    }else if(recognizer.state == UIGestureRecognizerStateChanged){
        //实时设置button的center、recognizer
        button.center = CGPointMake(button.center.x + translation.x, button.center.y + translation.y);
        [recognizer setTranslation:CGPointZero inView:button];
        //对button的位置进行判断，超出范围则强行使拖拽事件结束
//        if(button.center.x <= 40 || button.center.x>=Width-40 || button.center.y <=100 || button.center.y >=Height-100){
//            [recognizer setState:UIGestureRecognizerStateEnded];
//        }
         
    }else if(recognizer.state == UIGestureRecognizerStateEnded){
        //得到结束时button的center
        //根据center 判断应该的X和Y
//        CGFloat newX=button.center.x;
//        if(button.center.x <=Width/2) newX=40;
//        else if(button.center.x >=Width/2) newX=Width-40;
//         
//        CGFloat newY=button.center.y;
//        if(button.center.y <=100) newY=100;
//        else if(button.center.y >=Height-100) newY=Height-100;
//         
//        button.center = CGPointMake(newX, newY);
//        [recognizer setTranslation:CGPointZero inView:button];
    }
    if(button.tag == 1){
        _point1 =button.center;
    }else if (button.tag == 2){
        _point2 =button.center;
    }else if (button.tag == 3){
        _point3 =button.center;
    }else if (button.tag == 4){
        _point4 =button.center;
    }
    [self setNeedsDisplay];
}
-(CGPoint)getTLpoint{
    return self.buttonTL.center;
}
-(CGPoint)getTRpoint{
    return self.buttonTR.center;
}
-(CGPoint)getBLpoint{
    return self.buttonBL.center;
}
-(CGPoint)getBRpoint{
    return self.buttonBR.center;
}
@end
